package com.pzb.rabbitmq;

import com.pzb.common.utils.DateUtils;
import com.pzb.goods.domain.GdGoodsUnits;
import com.pzb.goods.service.IGdGoodsService;
import com.pzb.order.domain.OdOrder;
import com.pzb.order.domain.OdOrderDetail;
import com.pzb.order.service.IOdOrderDetailService;
import com.pzb.order.service.IOdOrderService;
import com.pzb.wechat.domain.WechatUserAccount;
import com.pzb.wechat.service.IWechatUserAccountService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@RabbitListener(queues = "mall.order.cancel.plugin")
public class RabbitMqReceiver {
    private static Logger LOGGER = LoggerFactory.getLogger(RabbitMqReceiver.class);
    @Autowired
    private IOdOrderService orderService;
    @Autowired
    private IOdOrderDetailService orderDetailService;
    @Autowired
    private IWechatUserAccountService userAccountService;
    @Autowired
    private IGdGoodsService goodsService;

    @RabbitHandler
    public void handle(RabbitMqModel mqModel, Channel channel, Message message) throws Exception{
        LOGGER.info("receive delay message orderId:{}", mqModel);
        OdOrder order = orderService.selectOdOrderByOrderCode(mqModel.getOrderNo());
        if(mqModel.getType() == 0){ //如果超时取消
            System.err.println("=================>接收到订单取消队列："+mqModel.getOrderNo()+"--"+ DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            if(order!=null){
                //如果是未支付状态  订单取消
                if("0".equals(order.getOrderStatus()) && "0".equals(order.getPayStatus())){
                    orderService.updateOrderStatus(order.getId(),"5");
                    //归还积分
                    if(order.getIntegral().longValue() > 0){
                        WechatUserAccount userAccount = userAccountService.selectWechatUserAccountByOpenId(order.getOpenid(), order.getExtAppid());
                        userAccount.setIntegral(userAccount.getIntegral() + order.getIntegral().intValue());
                        userAccountService.updateWechatUserAccount(userAccount);
                    }
                    //归还库存
                    List<OdOrderDetail> detailList = orderDetailService.selectOdOrderDetailByOrderCode(order.getOrderCode());
                    for (OdOrderDetail detail : detailList) {
                        GdGoodsUnits goodsUnit = goodsService.selectGoodsUnitsByGoodsIdAndUnits(detail.getGoodsId(), detail.getGoodsUnit());
                        if(goodsUnit!=null){
                            goodsUnit.setStock(goodsUnit.getStock()+detail.getQuantity());
                            goodsService.updateGdGoodsUnits(goodsUnit);
                        }
                    }
                }
            }
        }else if(mqModel.getType() == 1){//超时自动收货
            System.err.println("超时自动收货----"+mqModel.getOrderNo());
            if(order!=null) {
                //如果到期仍然为未收货状态
                if ("1".equals(order.getOrderStatus())) {
                    //设为已收货
                    order.setOrderStatus("2");
                    order.setFinishTime(new Date());
                    orderService.updateOdOrder(order);
                }
            }
        }
        //手动ack确认，表示消费者已经成功消费，另外肯定还有自动确认，只不过容易出现消息未消费则丢失的情况
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }
}
